package biz;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.Scanner;

import Utils.CardUtil;
import entity.MobileCard;
import entity.ServicePackage;

public class SosoMgr {
	static Scanner input = new Scanner(System.in);
	static CardUtil cardUtil = new CardUtil();

	//控制台输出
	public static void main(String[] args) throws IOException {
		cardUtil.init();
		do {
		System.out.println("*********************欢迎使用搜搜移动业务大厅****************");
		System.out.println("1.用户登录　２.用户注册 3.使用搜搜　4.话费充值　5.资费说明　6.退出系统");
		System.out.print("请选择：");
		int choose = input.nextInt();
		switch (choose) {
		case 1:
			System.out.print("请输入手机号：");
			String number = input.next();
			System.out.print("请输入密码：");
			String password = input.next();
			MobileCard res = cardUtil.isExistCard(number, password);
			if(res!=null) {
				login(res);
			}else {
				System.out.println("此手机号　不存在");
			}
			break;
		case 2:
			regist();
			break;
		case 3:
			System.out.println("请输入手机号");
			number = input.next();
			cardUtil.userSoso(number);
			break;
		case 4:
			System.out.println("请输入指定的手机号");
			number = input.next();
			System.out.println("请输入要充值的钱数");
			double price = input.nextDouble();
			money += price;
			while (price<50) {
				System.out.println("钱数不能小于５０　请重新输入");
				price = input.nextDouble();
			}
			cardUtil.chargeMoney(number, price);
			break;
		case 5:
			inputStreamMobile();
			break;
		case 6:
			System.out.println("程序退出");
			System.exit(0);
		default:
			System.out.println("输入错误");
			break;
		}
		}while(true);
	}
	static ServicePackage package1;
	static MobileCard card;
	static double money;
	static  void regist() {//用户注册
		String [] s = cardUtil.getNewNumbers(9);
		for (int i = 0; i < s.length; i++) {
			if(i%3==0) {
				System.out.println();
			}
			System.out.print((i+1)+"."+s[i]+" ");
			if(i==s.length-1) {
				System.out.println();
			}
		}
		System.out.print("请选择卡号:");
		int num = input.nextInt();
		while(num>s.length) {
			System.out.println("输入错误　请重新输入");
			num = input.nextInt();
		}
		String cardNumber =  s[num-1];
		System.out.println("选择套餐　　１．话唠套餐、２．网虫套餐、３．超人套餐");
		System.out.print("请选择:");
		int packId = input.nextInt();
		package1 = cardUtil.createPack(packId);
		if(package1!=null) {
			System.out.print("请输入用户名　");
			String user = input.next();
			System.out.print("请输入密码");
			String password = input.next();
			System.out.println("请输入预存话费金额");
			money =  input.nextDouble();
			while(booMoney(package1, money)) {
				System.out.println("钱数不足　请重新输入");
				money = input.nextDouble();
			}
			card = new MobileCard(cardNumber, user, password, package1, money-package1.getPrice());
			cardUtil.addCard(card);
			package1.showInfo();
		}
	}
	static boolean booMoney(ServicePackage package1, double money) {
		if(package1.getPrice()<money) {
			return false;
		}
		return true;
	}
	static void login(MobileCard res) throws IOException {
		do {
		System.out.println("１．本月账单查询");
		System.out.println("２．套餐余量查询");
		System.out.println("３．打印消费详情单");
		System.out.println("４．套餐变更");
		System.out.println("５. 办理退网");
		System.out.print("请选择（输入　１－５　选择功能　其他返回上一级）");
		int choose = input.nextInt();
		switch (choose) {
		case 1:
			System.out.println();
			System.out.println("××××××××本月账单查询××××××××");
			System.out.println("您的卡号为"+res.getCardNumber());
			System.out.println("当月账单为：");
			System.out.println("\t套餐资费为："+res.getSerPacage().getPrice());
			//合计　保留　１　位　小数
			double a = money-res.getAonsumAmount();
		    BigDecimal bigDecimal = new BigDecimal(a);
			System.out.println("\t合计："+bigDecimal);
			System.out.println("\t账户余额为："+res.getAonsumAmount());
			System.out.println();
			break;
		case 2:
			System.out.println("********套餐余量查询×××××××××");
			cardUtil.showRemainDetail(res.getCardNumber());
			break;
		case 3:
			cardUtil.printConsumInfo(res.getCardNumber());
			break;
		case 4:
			System.out.println("请选择您要更换的套餐１．话唠套餐、２．网虫套餐、３．超人套餐");
			choose = input.nextInt();
			switch (choose) {
			case 1:
				cardUtil.changingPack(res.getCardNumber(),"话唠套餐");
				break;
			case 2:
				cardUtil.changingPack(res.getCardNumber(),"网虫套餐");
				break;
			case 3:
				cardUtil.changingPack(res.getCardNumber(),"超人套餐");

				break;
			}
			break;
		case 5:
			cardUtil.delCard(res.getCardNumber());
			System.exit(0);
			break;
		default:
			return;
		}
		}while(true);
	}
	@SuppressWarnings("resource")
	public static void inputStreamMobile() throws IOException {
		FileInputStream stream = null;
		InputStreamReader isr = null;
		BufferedReader br = null;
		stream = new FileInputStream("资费说明.text");
		isr = new InputStreamReader(stream);
		br = new BufferedReader(isr);
		String lien;
		while((lien = br.readLine())!=null) {
			System.out.println(lien);
		}
	}
}
